package com.iursp.conf.aop;


import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;

import com.iursp.domain.model.logger.ServiceLogger;
import com.iursp.service.log.LoggerService;
import com.iursp.utils.MD5Util;
import com.iursp.utils.common.AnnotationUtils;

import javassist.NotFoundException;

@Component
@Aspect
public class LogAspect {
	
	@Autowired
	private LoggerService loggerService;

	private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.iursp.service..*(..))")
    private void pointcut() {
    }

    /**
     * 
     * @Title: After   
     * @Description: 记录业务日志--->插入数据库   
     * @param: @param joinPoint
     * @param: @throws NotFoundException
     * @param: @throws ClassNotFoundException      
     * @return: void      
     * @throws
     */
    @After(value = "pointcut()")
    public void After(JoinPoint joinPoint) throws NotFoundException, ClassNotFoundException {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        String logStr = AnnotationUtils.get().getAnnotatioinFieldValue(className, methodName, LoggerManage.class.getName(), "logStr");
        logger.info("业务层逻辑日志className="+className+"methodName="+methodName+"logStr="+logStr);
        if (!StringUtils.isEmpty(logStr)) {
            logger.error("获取日志：" + logStr);
            ServiceLogger serviceLogger = new ServiceLogger();
        	serviceLogger.setSystemName(className);
        	serviceLogger.setLogContent(logStr);
        	serviceLogger.setLogLevel("info");
        	serviceLogger.setPerformTime(new Date());
        	serviceLogger.setRequestNumber(MD5Util.encrypt());
        loggerService.saveServiceLogger(serviceLogger);
        }
     
        
    }
}
